﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace JWTToken授权验证.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class AuthenticationController : ControllerBase
    {
        //jwt服务
        private readonly IJWTService _jWTService;
        public AuthenticationController(IJWTService jWTService)
        {
            _jWTService = jWTService;
        }

        /// <summary>
        /// 根据用户名和密码获取token
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpPost]
        public string login(string userName,string password)
        {
            if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
            {
                if(string.Compare(userName,"abc",true)==0&&string.Compare(password,"123",true)==0)
                {
                    string token = _jWTService.GetToken(userName,password);
                    return JsonConvert.SerializeObject(new
                    {
                        Result = true,
                        Token = token
                    });
                }
            }
            return string.Empty;
        }
    }
}